package org.atovio.mvcConfig.interceptor;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;

/**
 * 同样的，需要到MVCSettings类中注册，难道就没注解方式？我不信。可以，我信了
 * @author AtoVio, Winter Yuan
 */
public class AuthInterceptor implements HandlerInterceptor {

    private static final String COMMON_USER = "Violet";

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("=== AuthInterceptor === 执行了");

        // 获取请求中的用户名
        String username = request.getParameter("username");

        // 获取URI
        String uri = request.getRequestURI();

        // 获取请求类型
//        String method = request.getMethod();

        // 判断用户操作
        if(COMMON_USER.equals(username) &&
                (uri.equals("/articles/add")) ||
                (uri.equals("/articles/query")) ||
                (uri.equals("/articles/update"))
        ){
            return false;
        }
        return true;
    }
}
